Conditions | 19 |
Total Lines | 54 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like helper.ts ➔ sortRankings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import { RankingDataTeamObject } from "../Types/Ranking" |
||
27 | |||
28 | export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) { |
||
29 | // Rank lager: A stijgt in sortering. |
||
30 | if (a.rank < b.rank) { |
||
31 | return -1 |
||
32 | } |
||
33 | if (a.rank > b.rank) { |
||
34 | return 1 |
||
35 | } |
||
36 | // Aantal overwinningen hoger: A stijgt in sortering. |
||
37 | if (a.wins > b.wins) { |
||
38 | return -1 |
||
39 | } |
||
40 | if (a.wins < b.wins) { |
||
41 | return 1 |
||
42 | } |
||
43 | // Doelpuntensaldo beter: A stijgt in sortering. |
||
44 | if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) { |
||
45 | return -1 |
||
46 | } |
||
47 | if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) { |
||
48 | return 1 |
||
49 | } |
||
50 | // Aantal gemaakte doelpunten hoger: A stijgt in sortering. |
||
51 | if (a.goalsScored > b.goalsScored) { |
||
52 | return -1 |
||
53 | } |
||
54 | if (a.goalsScored < b.goalsScored) { |
||
55 | return 1 |
||
56 | } |
||
57 | // Aantal uitoverwinningen hoger: A stijgt in sortering. |
||
58 | if (a.winsAway > b.winsAway) { |
||
59 | return -1 |
||
60 | } |
||
61 | if (a.winsAway < b.winsAway) { |
||
62 | return 1 |
||
63 | } |
||
64 | // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering. |
||
65 | if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) { |
||
66 | return -1 |
||
67 | } |
||
68 | if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) { |
||
69 | return 1 |
||
70 | } |
||
71 | // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering. |
||
72 | if (a.goalsScoredAway > b.goalsScoredAway) { |
||
73 | return -1 |
||
74 | } |
||
75 | if (a.goalsScoredAway < b.goalsScoredAway) { |
||
76 | return 1 |
||
77 | } |
||
78 | |||
79 | return a.team?.club?.localName.localeCompare(b.team?.club?.localName) |
||
80 | } |
||
81 | |||
142 |